package com.twu.biblioteca;
import java.io.*;
import java.util.Formatter;

public class BibliotecaApp {
    public static void main(String[] args) {

        User u[]= new User[5];
        User u1=new User(0);
        Movie m[]=new Movie[15];
        Movie m1=new Movie(0);
        Book b[] = new Book[20];
        Book b1=new Book(0);
        System.out.println("Welcome to Biblioteca!!");
        BibliotecaApp lib = new BibliotecaApp();
        for(int i=0;i<5;i++)
        {
            u[i]=new User(u1.getUsername(i),u1.getName(i),u1.getPwd(i),u1.getEmailID(i),u1.getPhno(i));
        }
        for(int i=0;i<15;i++)
        {
            m[i]=new Movie(m1.getMovname(i),m1.getYr(i),m1.getDir(i),m1.getRating(i));
        }
        for(int i=0;i<20;i++)
        {
            b[i]=new Book(b1.getBookName(i));
        }
        lib.menu(b,m,u);
        System.out.println("Thank you for using Biblioteca! Visit again...");
    }

    //This method displays the menu of the program and calls appropriate method depending on choice of user
    public void menu(Book b[],Movie m[],User u[])
    {
        int choice=0;
        User result;
        try{
             do{
                  System.out.println("\n\nPlease enter your choice:");
                  System.out.println("1. View list of books\n2. Reserve a book\n3. Check membership details\n" +
                          "4. View movie details\n5. Exit\n");
                  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                  choice=Integer.parseInt(br.readLine());
                  switch(choice)
                  {
                    case 1:    viewList(b);  //To view list of books
                               break;

                    case 2:    result=authenticateUser(u);  //authenticates user
                               if(result!=null)
                                  reserveBook(b);       //book can be reserved only by authenticated user
                               break;

                    case 3:    result=authenticateUser(u);
                               checkMemDetails(result);    //checks membership details of user
                               break;

                    case 4:    System.out.println("Enter the name of the movie: ");
                               br=new BufferedReader(new InputStreamReader(System.in));
                               viewMovieDetails(m,convertStreamToString(br));     //view movie details
                               break;

                    case 5:    break;

                    default: System.out.println("\nSelect a valid option.\n"); //invalid option entered by user
                  }
                }while(choice!=5);

            }
        catch(IOException ie)
        {
          ie.printStackTrace();
        }
        catch(NullPointerException npe)
        {
            npe.printStackTrace();
        }
    }

    //This method authenticates the user
    public User authenticateUser(User u[])
    {
      String uname, pwd;

      System.out.println("\nEnter your username: ");
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      uname=convertStreamToString(br);

      System.out.println("\nEnter your password: ");
      br=new BufferedReader(new InputStreamReader(System.in));
      pwd=convertStreamToString(br);

      User res;
      res = checkUserList(u, uname, pwd);
      if(res==null)   //User not registered
      {
          System.out.println("\nIncorrect username or password!!\n");
      }
      return(res);
}

    //This method checks for user credentials entered against those of registered users
    public User checkUserList(User u[], String uname, String pwd)
    {
       for(int i=0;i<5;i++)
       {
          if(u[i].isRegisteredUser(uname, pwd)==1)
          {
              return(u[i]);
          }
       }
       return null;
    }

    //This method displays list of books available
    public void viewList(Book b[])
    {
       for(int i=0;i< 20; i++)
       {
           System.out.println(b[i].getBookName(i));
       }
       System.out.println("\n");
    }

    //This method reserves book for a user
    public void reserveBook(Book b[])
    {
        try{
             System.out.println("Enter the name of the book to be reserved");
             BufferedReader bkName=new BufferedReader(new InputStreamReader(System.in));
             String Name=convertStreamToString(bkName);
             if(bookPresent(b,Name)==1)     //book is available
             {
               System.out.println("Thank You! Enjoy the book.");
             }
             else               //book bot available
             {
               System.out.println("Sorry we don't have that book yet.");
             }
           }
           catch(Exception e)
           {
              e.printStackTrace();
           }
    }

   //This method checks membership details of user
    public void checkMemDetails(User u)
    {
        String col1="Name",col2="Email ID",col3="Phone No";
        if(u==null) //not authenticated
           System.out.println("Please talk to a Librarian. Thank you.\n\n");
        else  //user authenticated
        {
            System.out.format("\n%-20s%-20s%-15s", col1, col2, col3);
            System.out.format("\n%-20s%-20s%-15s",u.getName(u.returnUserIndex(u)), u.getEmailID(u.returnUserIndex(u)), u.getPhno(u.returnUserIndex(u)));
        }
    }

    //This method checks if the book requested is available or not
    public int bookPresent(Book b[], String bkName)
    {
       int i=0;
       for(i=0;i<20;i++)
       {
           if(bkName.equals(b[i].getBookName(i)))
               return(1);
       }
       return(0);
    }

    //This method converts BufferedReader to String
    public String convertStreamToString(BufferedReader is)
    {
       java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\n");
          return s.hasNext() ? s.next() : "";
    }

    //This method checks if the movie requested is available or not
    public int moviePresent(Movie m[],String movName)
    {
        for(int i=0;i<15;i++)
        {
            if(movName.equals(m[i].getMovname(i)) )
            return(i);
        }
        return(-1);
    }

    //This method displays the details of a movie to the user
    public void viewMovieDetails(Movie m[],String movieName)
    {
       String format="%-40s%-6d%-30s%-3s\n";
       String col1="Movie", col2="Year", col3="Director", col4="Rating";
       int k=moviePresent(m,movieName);
        if(k!=-1)
        {
            System.out.format("\n%-40s%-6s%-30s%-3s\n", col1, col2, col3, col4);
            System.out.format(format, m[k].getMovname(k), m[k].getYr(k), m[k].getDir(k), m[k].getRating(k));
        }
        else
            System.out.println("\nSorry, the movie you requested is not available!");
    }
}





